package com.hspedu.tankgame4;

import java.util.Vector;

/**
 * 敌人的tank
 */
public class EnemyTank extends Tank implements Runnable {

    Vector<Shot> shots = new Vector<>();

    boolean isLive = true;

    public EnemyTank(int x, int y) {
        super(x, y);
    }

    @Override
    public void run() {

        while (true) {

            if (isLive && shots.size() < 1) {
                //这里我们判断如果shots size() < 1创建一颗子弹,放入到shots集合,并启动
                Shot s = null;

                switch (getDirect()) {
                    case 0: //上
                        s = new Shot(getX() + 20, getY(), 0);
                        break;
                    case 1:  //右
                        s = new Shot(getX() + 60, getY() + 20, 1);
                        break;
                    case 2:  //下
                        s = new Shot(getX() + 20, getY() + 60, 2);
                        break;
                    case 3:  //左
                        s = new Shot(getX(), getY() + 20, 3);
                        break;
                }

                shots.add(s);
                new Thread(s).start();

            }


            switch (getDirect()) {

                case 0:  //上
                    for (int i = 0; i < 30; i++) {
                        if (getY() > 0) {
                            moveUp();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1:  //右
                    for (int i = 0; i < 30; i++) {
                        if (getX() + 60 < 1000) {
                            moveRight();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2:  //下
                    for (int i = 0; i < 30; i++) {
                        if (getY() + 60 < 750) {
                            moveDown();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3:  //左
                    for (int i = 0; i < 30; i++) {
                        if (getX() > 0) {
                            moveLeft();
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }


            //然后随机的改变坦克方向 0-3
            setDirect((int) (Math.random() * 4));
            //setDirect(3);
            //写并发程序，一定要考虑清楚，该线程什么时候结束
            if (!isLive) {
                break;
            }

        }
    }
}























